<template>
  <div>
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      class="login-container"
      label-position="left"
      label-width="0px"
      v-loading="loading"
    >
      <h3 class="login_title">系统登录</h3>
      <el-form-item prop="username">
        <el-input type="text" auto-complete="off" placeholder="账号" v-model="ruleForm.username"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input type="password" auto-complete="off" placeholder="密码" v-model="ruleForm.password"></el-input>
      </el-form-item>

      <el-form-item prop="code">
        <div class="el-form-item__content" style="margin-left: 0px;">
          <div class="el-row" span="24">
            <div class="el-col el-col-16">
              <div class="el-input el-input--prefix">
                <!---->
                <input
                  type="text"
                  autocomplete="off"
                  maxlength="4"
                  placeholder="请输入验证码"
                  class="el-input__inner"
                  v-model="ruleForm.code"
                />
              </div>
            </div>
            <div class="el-col el-col-8">
              <div class="login-code">
                <img :src="codeUrl" class="login-code-img" @click="codeOnClick()"/>
              </div>
            </div>
          </div>
        </div>
      </el-form-item>
      <el-checkbox class="login_remember" v-model="checked" label-position="left">记住密码</el-checkbox>
      <el-button type="text" style="width: 50%" @click="setTwicePassword()">重置密码</el-button>

      <el-form-item style="width: 100%">
        <el-button type="primary" style="width: 100%" @click="submitClick()">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import store from "./store";
export default {
  data() {
    return {
      loading: false,
      checked: false,
      codeUrl: "http://localhost:10010/code?randomStr=128989898",
      randomStr: "",
      loginS: "",
      ruleForm: {
        username: "",
        password: "",
        randomStr: "",
        code: ""
      },
      rules: {
        username: [
          { required: true, message: "请输入名称", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 10 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 10 个字符", trigger: "blur" }
        ],
        code: [
          { required: true, message: "请输入验证码", trigger: "blur" },
          { min: 4, max: 4, message: "长度不能少于4个字符", trigger: "blur" }
        ]
      }
    };
  },
  mounted: function() {
    this.getCookie();
    this.codeOnClick();
  },
  methods: {
    codeOnClick() {
      //获取时间戳
      var randomStr = new Date().getTime();
      console.log("当前时间戳"+randomStr)
      this.codeUrl = "http://localhost:10010/code?randomStr=" + randomStr;
      this.ruleForm.randomStr = randomStr;
    },
    submitClick: function() {
      var $this = this;
      if(this.loginS>3){
        var randomStr = new Date().getTime(); 
        var nowTime = randomStr.toLocaleString();
      }

      $this
        .$ajax({
          url: "http://localhost:10010/oauth/login",
          method: "post",
          params: $this.ruleForm
        })
        .then(function(res) {
          console.log("登录返回----" + res.data.code);

          $this.$message(res.data.msg);
          if (res.data.code == 200) {
            if ($this.checked) {
              $this.setCookie($this.ruleForm.username, $this.ruleForm.password);
            }
            //通过VUEX保存状态s
            store.commit("login", res.data.object);
            //路由跳转
            $this.$router.replace({ path: "/home" });
          } else {
            $this.loginS = $this.loginS + 1;

            $this.clearCookie();
          }
        });
    },
    keepLoginError:function(){
      var userLogin = this.ruleForm.username;
      var Error=this.loginS;

    },
    setCookie(c_name, c_password, exdays) {
      //获取时间
      var exdate = new Date();
      //设置保存的天数
      exdate.setTime(exdate.getTime() + 20 * 60 * 60 * 100 * exdays);

      window.document.cookie =
        "username" + "=" + c_name + ";path=/;expires=" + exdate.toGMTString();
      window.document.cookie =
        "password" +
        "=" +
        c_password +
        ";path=/;expires=" +
        exdate.toGMTString();
    },
    getCookie: function() {
      var wmh = this;
      //打印cookie
      console.log(document.cookie);

      if (document.cookie.length > 0) {
        var arr = document.cookie.split(";");
        for (var i = 0; i < arr.length; i++) {
          var arr2 = arr[i].split("=");
          /*alert(arr2[0]+"000")
              alert(arr2[1]+"111")*/

          if (arr2[0] == " password") {
            wmh.ruleForm.password = arr2[1];
          }

          if (arr2[0] == "username") {
            wmh.ruleForm.username = arr2[1];
          }
        }
      }
    },
    clearCookie: function() {
      this.setCookie("", "", -1);
    },
    setTwicePassword: function() {
      this.$router.replace({ path: "/reloadPassword" });
    }
  }
};
</script>
<style>
.login-container {
  border-radius: 15px;
  background-clip: padding-box;
  margin: 180px auto;
  width: 350px;
  padding: 35px 35px 15px 35px;
  background: #fff;
  background: url("./assets/zsh.jpg");
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
  opacity: 8;
}
.login_title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
  opacity: 8;
}
.login_remember {
  margin: 0px 0px 35px 0px;
  text-align: left;
  opacity: 8;
}
</style>